import { Injectable } from '@nestjs/common';
import { sms } from 'tencentcloud-sdk-nodejs';
import { ConfigService } from '@nestjs/config';
import { Class_HttpException_Base } from '@/common/classes/httpException';
import { SmsServiceService_Send_SeClass } from './assets/smsService.class';

@Injectable()
export class SmsService_Service {
  private smsClient: any;
  Client = sms.v20210111.Client;
  constructor(private configService: ConfigService) {
    this.smsClient = new this.Client({
      profile: {
        signMethod: 'HmacSHA256',
      },
      credential: {
        secretId: this.configService.get<string>('SMS_SECRETID'),
        secretKey: this.configService.get<string>('SMS_SECRETKEY'),
      },
      region: this.configService.get<string>('SMS_REGION'),
    });
  }

  async smsService_Send(data: SmsServiceService_Send_SeClass) {
    const params = {
      SmsSdkAppId: Number(this.configService.get<number>('SMS_SMSSDKAPPID')),
      SignName: this.configService.get<string>('SMS_SIGNNAME'),
      PhoneNumberSet: [`+86${data.phone}`],
      TemplateId: Number(this.configService.get<number>('SMS_TEMPLATEID')),
      TemplateParamSet: [data.code, 2],
    };
    const res = await this.smsClient.SendSms(params);
    if (res.SendStatusSet[0].Code === 'Ok') {
      return 'OK';
    }
    throw new Class_HttpException_Base(res.SendStatusSet[0].Code);
  }
}
